#include<stdio.h>
#include<stdlib.h>
#include "````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````



















































































































\../include/linkedlist.h"

void performOperation(struct node * , int);
int getMenuChoice();

void performOperation(struct node * start, int choice)
{
	label1:
	switch(choice)
	{
		case 1:
			insertNewNode(start);
			break;
			
		case 2:
			start=(struct node *)deleteNode(start);
			break;
			
		case 3:
			start=(struct node *)displayOrGetLLFromFile();
			break;
			
		case 4:
			exit(0);
			
		default:
			printf("Invalid Input..");
	}
	
	printf("Press -1 to continue to Menu? ");
	scanf("%d", &choice);
	
	if(choice == -1)
	{
		goto label1;
	}
	
}

void insertNewNode(struct node *start)
{
FILE *fp;
	int x;
	fp=fopen("Desktop:\\files\\linkedlist.txt", "wb");
	struct node *p, *q;
	q=start;
	int node1, nodedata;
	start=(struct node *)displayOrGetLLFromFile();
	printf("\nEnter the data you want to insert:  ");
	scanf("%d", &nodedata);
	printf("\nEnter the node after which you wish to insert node:  ");
	scanf("%d", &node1);
	p=(struct node * )malloc(sizeof(struct node));
	p->data=nodedata;
	while(q->data!=nodedata && q->next!=NULL)
	{
		q=q->next;
	}
	if(q->data == nodedata)
	{
		p->next=q->next;
		q->next=p;
		printf("The node is successfully inserted.");
		q=start;
		while(q!=NULL)
		{
			q=q->next;
			x=q->data;
			fwrite(&x, sizeof(int), 1, fp);
		}
	}
	else
	{
		printf("Operation Terminated! The data node you enetered does not exist in the Linked List");
	}
	fclose(fp);
}

struct node * deleteNode(struct node *start)
{
FILE *fp;
	int x;
	fp=fopen("Desktop:\\files\\linkedlist.txt", "wb");
	struct node *p, *q;
	int nodedata;
	p=start;
	printf("Enter the node data you wish to delete:  ");
	scanf("%d", &nodedata);
	if(p->data==nodedata)
	{
		q=p;
		p=p->next;
		start=p;
		free(q);
		
		q=start;
		while(q!=NULL)
		{
			x=q->data;
			
			q=q->next;
			
			fwrite(&x, sizeof(int), 1, fp);
		}
	}
	else
	{
		while(p->next->data!=nodedata && p->next!=NULL)
		{
			p=p->next;
		}
		if(p->next->data==nodedata)
		{
			q=p->next;
			p->next=p->next->next;
			free(q);
		
			q=start;
			while(q!=NULL)
			{
				x=q->data;
				q=q->next;
				fwrite(&x, sizeof(int), 1, fp);
			}
		}
		else
		{
			printf("Operation Terminated! The node data you entered does not exist in the Linked List.");
		}
	}
	fclose(fp);
	return start;
}

int getMenuChoice()
{
	int choice;
	printf("\n\nSelect your choice from Menu:  \n\n");
	printf("1. Insert node in LL\n2. Delete node from LL\n3. Display LL\n4. Exit");
	scanf("%d", &choice);
	return choice;
}

struct node* displayOrGetLLFromFile()
{
	int a;
	int count=1;
	struct node *p, *q, *start;

	FILE *fpRead;



	fpRead=fopen("K:\\files\\linkedlist.txt", "rb");
	while(fread(&a, sizeof(int), 1, fpRead))
	{
			if(count==1)
			{
				start=(struct node*)malloc(sizeof(struct node));
				start->data=a;
				start->next=NULL;
			}
			else
			{
				insert(start, a);
			}	
			count++;			
	}
	
	printf("The linked list in the binary file is:  \n");
	while(start!=NULL)
	{
		printf("%d  ", start->data);
		start=start->next;
	}	
	fclose(fpRead);		
	
	return start;
}

void insert(struct node * start, int a)  // Reading data from file one by one and creating a LL out of it
{
	struct node *p;
	p=(struct node*)malloc(sizeof(struct node));
	p->data=a;
	p->next=NULL;
	while(start->next!=NULL)
	{
		start=start->next;
	}
	start->next=p;
}


void main()
{

	int choice;
	struct node *start;
	
	start=displayOrGetLLFromFile();
	
	choice=getMenuChoice();
	
	performOperation(start, choice);
	
	printf("Exiting from Main..!");
	
	

}
